package org.ndx.majick.ui.bool;

import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;

import org.ndx.majick.properties.Property;
import org.ndx.majick.ui.IClassUIProvider;
import org.ndx.majick.ui.IPropertyUIMetadata;
import org.ndx.majick.ui.object.ObjectUIProvider;

public class BooleanUIProvider extends ObjectUIProvider implements IClassUIProvider {

	public BooleanPropertyMetadata createMetadata() {
		return new BooleanPropertyMetadata();
	}

	public JComponent getEditor(IPropertyUIMetadata metadata, Property<?> source) {
		JComponent returned = getComponent(metadata, source, true);
		configureEditor(returned, metadata, source);
		return returned;
	}

	private JComponent getComponent(IPropertyUIMetadata metadata,
			Property<?> source, boolean edit) {
		BooleanPropertyMetadata boolMeta = (BooleanPropertyMetadata) metadata;
		/* Yup, anyway, I consider returned object to be an abstract button, be it a JToggleButton, a JCheckBoxMenuItem, or a JCheckBox */
		AbstractButton used = null;
		switch(boolMeta.getRendering().get()) {
		case JCHECKBOX:
			used = new JCheckBox();
		}
		BooleanHandler handler = new BooleanHandler((Property<Boolean>)source, used);
		if(edit) {
			used.addActionListener(handler);
			used.addChangeListener(handler);
		}
		return used;
	}

	public JComponent getViewer(IPropertyUIMetadata metadata, Property<?> source) {
		JComponent returned = getComponent(metadata, source, true);
		configureViewer(returned, metadata, source);
		returned.setEnabled(false);
		return returned;
	}

}
